import stockApi from "@/api/store/stock";
import dictApi from "@/api/system/dict";
import houseApi from "@/api/store/house";
import putApi from "@/api/store/put";
export default {
  name: "stock",
  created() {
     this.init();
  },
  computed:{
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        current: 1,
        size: 10,
        beginTime: "",
        endTime: "",
        criteria: {
          inBoundNo: "",
          inBoundStatus: "",
          inBoundResponsible: "",
          orderNum: "",
        },
      },
      formVisible:false,
      timeArray: [],
      goodsDetailList: [],
      baseUrl:process.env.VUE_APP_BASE_API,
    };
  },
  methods: {
    init() {
      this.fetchData();
      this.getTypeDict();
    },
    fetchData() {
      this.listLoading = true;
      if(this.timeArray.length>1){
        this.listQuery.beginTime = this.timeArray[0]
        this.listQuery.endTime = this.timeArray[1]
      }
      stockApi
        .getList(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.listLoading = false;
          this.total = response.data.total;
        })
        .catch(() => {});
    },
    formatDate(originVal) {
        const dt = new Date(originVal);
        const y = dt.getFullYear();
        const m = (dt.getMonth() + 1 + "").padStart(2, "0");
        const d = (dt.getDate() + "").padStart(2, "0");
        const hh = (dt.getHours() + "").padStart(2, "0");
        const mm = (dt.getMinutes() + "").padStart(2, "0");
        const ss = (dt.getSeconds() + "").padStart(2, "0");
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
    generNum() {
      return Math.floor(Math.random() * 20);
    },
    getTypeDict() {
      // 从后台获取字典列表
      let dictName = "商品类型";
      dictApi.getDicts(dictName).then((response) => {
        this.goodsTypeList = response.data[0].children ?? [];
      });
    },
    getTypeValueDict(num) {
      const arr = this.goodsTypeList;
      if (arr) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].num == num) {
            return arr[i].name;
          }
        }
      }
    },
    generNum() {
        return Math.floor(Math.random() * 20);
      },
    search() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    reset() {
        this.timeArray = []
      this.listQuery.criteria.inBoundStatus = "";
      this.listQuery.criteria.inBoundResponsible = "";
      this.listQuery.criteria.inBoundNo = "";
      this.listQuery.criteria.orderNum = "";
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleClose() {},
    fetchNext() {
      this.listQuery.current = this.listQuery.current + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.current = this.listQuery.current - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.current = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.size = limit;
      this.fetchData();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },
    showDetail(id, type) {
          if (!id) {
            return;
          }
          //根据type查询详情
          
            putApi.getGoodsDetail(id, type).then((response) => {
              this.goodsDetailList = response.data;
              this.formVisible = true;
            });
          
        },
  },
};
